package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.commons.constant.TlPayConstant;
import cc.linker.steplink.crm.commons.constant.WcPayConstant;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.WcPayProperties;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchant.dao.acs.LinkAccountDao;
import cc.linker.steplink.crm.merchant.service.acs.LinkAccountService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

@Service
public class LinkAccountServiceImpl implements LinkAccountService {

    @Autowired
    private LinkAccountDao linkAccountDao;

    @Autowired
    private HttpService httpService;

    @Autowired
    private WcPayProperties wcPayProperties;

    @Value("${system.scrm.manageweb}")
    private String managewebUrl;

    @Override
    public List getList() throws IOException, URISyntaxException {
        List<Map<String, Object>> list = linkAccountDao.getList();
        List<Map<String, String>> orgList = this.getOrgList();
        for (Map<String, Object> item : list) {
            if (!"40".equals(item.get("openStatus"))) {
                if (1 == (Integer) item.get("authStatus")) {
                    item.put("openStatus", "30");
                }
                if (0 == (Integer) item.get("authStatus")) {
                    item.put("openStatus", "20");
                }
            }
            item.put("ascription", linkAccountDao.getPlatformNameByOrgId((String) item.get("orgId")));
            item.put("inMoney", NumberUtils.fenToYuan(linkAccountDao.getInMoneyByCustomerId((String) item.get("customerId"))));
            item.put("orgName", "");
            for (Map<String, String> orgItem : orgList) {
                if (!StringUtils.isEmpty(item.get("orgId")) && item.get("orgId").equals(orgItem.get("orgId"))) {
                    item.put("orgName", orgItem.get("orgName"));
                }
            }
        }
        return list;
    }

    @Override
    public List getLinkList(Integer platformId) {
        return linkAccountDao.getLinkList(platformId);
    }

    /**
     * 获取详情
     *
     * @param accountRecordid
     * @return
     */
    @Override
    public Map getInfo(Integer accountRecordid) {
        return linkAccountDao.getInfo(accountRecordid);
    }

    @Override
    public String getCustomerIdByAdminId(String adminId) {
        return linkAccountDao.getCustomerIdByAdminId(adminId);
    }

    @Override
    public String getOrgIdByCustomerId(String customerId) {
        return linkAccountDao.getOrgIdByCustomerId(customerId);
    }

    @Override
    public Boolean sendVerificationCode(Map<String, String> param) throws IOException, URISyntaxException {
        String result = httpService.doPost(wcPayProperties.getAcsurl() + TlPayConstant.USER_SEND_VERIFICATION_CODE, JSONObject.toJSONString(param), WcPayConstant.JSON);
        JSONObject object = JSONObject.parseObject(result);
        if (object != null) {
            if (("200").equals(object.getString("code"))) {
                return true;
            } else {
                throw GlobalException.build(object.getString("msg"));
            }
        }
        return false;
    }

    @Override
    public Map tosigncontract(Map<String, String> param) throws IOException, URISyntaxException {
        String result = httpService.doPost(wcPayProperties.getAcsurl() + TlPayConstant.USER_BIND_PHONE, JSONObject.toJSONString(param), WcPayConstant.JSON);
        if (!StringUtils.isEmpty(result)) {
            Map res = (Map) JSON.parse(result);
            if ("200".equals(res.get("code"))) {
                return (Map) res.get("data");
            } else {
                throw GlobalException.build((String) res.get("msg"));
            }
        }
        throw GlobalException.build("系统繁忙，请稍后再试(err=yzsj)");
//        JSONObject object = JSONObject.parseObject(result);
//        if(object!=null){
//            if(("200").equals(object.getString("code"))){
//                return (Map)object.get("data");
//            }else{
//                throw GlobalException.build(object.getString("msg"));
//            }
//        }
//        throw GlobalException.build("系统繁忙，请稍后再试(err=yzsj)");
    }

    //管理中心获取所有机构
    @Override
    public List getOrgList() throws IOException, URISyntaxException {
        String url = managewebUrl + "UserWebApi/api/org/all?paymode=2";
        String ret = httpService.doGet(url);
        if (StringUtils.isEmpty(ret)) {
            throw GlobalException.build("系统繁忙，请稍后再试(err=glzx)");
        }
        Map res = (Map) JSON.parse(ret);
        if (0 != (Integer) res.get("retcode")) {
            throw GlobalException.build((String) res.get("msg"));
        }
        Map retobj = (Map) res.get("retobj");
        List<Map<String, String>> orgList = (List) retobj.get("orglist");

        for (Map<String, String> item : orgList) {
            item.put("orgName", item.get("orgname"));
            item.put("orgId", item.get("orgcode"));
            item.remove("orgcode");
            item.remove("orgname");
            item.remove("domaincode");
        }
        return orgList;
    }
}
